*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Creating Tables for FD, AB and ML-SEM models					~~~~
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Creating matrix with estimated coefficients and standard errors	
	matrix mean = J(2,18,.)		
	qui: sum FDcon
	matrix mean[1,1]=r(mean)
	qui: sum FDconse
	matrix mean[1,2]=r(mean)
	qui: sum FDlag
	matrix mean[2,3]=r(mean)
	qui: sum FDlagse
	matrix mean[2,4]=r(mean)	
	qui: sum FDboth_con
	matrix mean[1,5]=r(mean)
	qui: sum FDboth_conse
	matrix mean[1,6]=r(mean)	
	qui: sum FDboth_lag
	matrix mean[2,5]=r(mean)
	qui: sum FDboth_lagse
	matrix mean[2,6]=r(mean)	
	qui: sum ABcon
	matrix mean[1,7]=r(mean)
	qui: sum ABconse	
	matrix mean[1,8]=r(mean)	
	qui: sum ABlag
	matrix mean[2,9]=r(mean)
	qui: sum ABlagse	
	matrix mean[2,10]=r(mean)	
	qui: sum ABboth_con
	matrix mean[1,11]=r(mean)
	qui: sum ABboth_conse
	matrix mean[1,12]=r(mean)	
	qui: sum ABboth_lag
	matrix mean[2,11]=r(mean)
	qui: sum ABboth_lagse
	matrix mean[2,12]=r(mean)
	qui: sum MLSEMcon
	matrix mean[1,13]=r(mean)
	qui: sum MLSEMconse
	matrix mean[1,14]=r(mean)	
	qui: sum MLSEMlag
	matrix mean[2,15]=r(mean)
	qui: sum MLSEMlagse
	matrix mean[2,16]=r(mean)	
	qui: sum MLSEMboth_con
	matrix mean[1,17]=r(mean)
	qui: sum MLSEMboth_conse
	matrix mean[1,18]=r(mean)	
	qui: sum MLSEMboth_lag
	matrix mean[2,17]=r(mean)
	qui: sum MLSEMboth_lagse
	matrix mean[2,18]=r(mean)	
	
* Non Converged ML-SEM Models
	qui: egen nonconv_MLSEMcon = rowmiss(MLSEMcon)	
	qui: sum nonconv_MLSEMcon
	global conv_MLSEMcon = 100 - (r(mean)*100)	
	qui: egen nonconv_MLSEMlag = rowmiss(MLSEMlag)		
	qui: sum nonconv_MLSEMlag
	global conv_MLSEMlag = 100 - (r(mean)*100)		
	qui: egen nonconv_MLSEMboth = rowmiss(MLSEMboth_con)		
	qui: sum nonconv_MLSEMboth
	global conv_MLSEMboth = 100 - (r(mean)*100)	
			
* True values of x and xlag
	local truex = $betax * (1-$lambdax)
	local truexlag = $betaxlag * $lambdax	

* Saving information about scenario
	local obs 		= $obs
	local waves	 	= $waves
	local lambdax	= $lambdax
	local endo		= $endo
	local unob		= $unob
	
* Saving Table
	frmttable using "$results\n`obs'-t`waves'-x`truex'-xlag`truexlag'-lambdax`lambdax'-endo`endo'-unob`unob'-FD-AB-MLSEM",statmat(mean) substat(1)						///
		title("Results of $reps Simulations (n=$obs, t=$waves)"											///
		"True effects: x=`truex'; xlag=`truexlag'"														///
		"lambdax=$lambdax | autox=$autox | autoy=$autoy | endo=$endo | unob=$unob")						///
		ctitles("Effect" "FD" "FD" "FD" "AB" "AB" "AB" "MLSEM" "MLSEM" "MLSEM")							///
		rtitle("x"\""\"xlag"\"")																		///
		note("Robust SE in Parentheses." 																///
		"Convergence: MLSEMcon=$conv_MLSEMcon%; MLSEMlag=$conv_MLSEMlag%; MLSEMboth=$conv_MLSEMboth%")	///												
		landscape replace 


